<?php

	// +----------------------------------------------------------------------
	// | 添加修改商品类目
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\store;

	use app\common\event\Base;

	use app\model\store\GoodCategory as GoodCategoryModel;
	use app\model\ucenter\User as UserModel;
	use think\Exception;

	class GoodCategory extends Base
	{


		/**
		 * 获取当前和子集id
		 * @param $id
		 * @return mixed
		 */
		public static function getChildWithIds($id)
		{
			$ids = GoodCategoryModel::where('parent_id', $id)->where('status', 1)->column('id');
			if ($ids) {
				$ids2 = GoodCategoryModel::where('parent_id', 'in', $ids)->where('status', 1)->column('id');
				if ($ids2) {
					$ids = array_merge($ids, $ids2);
				}
			} else {
				$ids = [$id];
			}
			return $ids;
		}

		/**
		 * 删除数据
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/6/30 11:35
		 */
		public function del($param, $user = null)
		{
			try {

				$del = GoodCategoryModel::where('id', 'in', explode(',', $param['id']))->select();

				foreach ($del as $item) {
					$item['status'] = -2;
					$item->save();
					if (2 == $item['level']) {
						GoodCategoryModel::where('parent_id', $item['id'])->setField('status', -2);
					} else {
						$items = GoodCategoryModel::where('parent_id', $item['id'])->where('status', 1)->select();
						foreach ($items as $item2) {
							$item2['status'] = -2;
							$item2->save();
							GoodCategoryModel::where('parent_id', $item2['id'])->setField('status', -2);
						}
					}
				}
				// 第1步 检测禁止发布的情况
				$data['code'] = 0;
				$data['message'] = '删除成功';
			} catch (Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		public static function getOptions()
		{
			$level1 = GoodCategoryModel::where('status', 1)->where('parent_id', 0)->field('id,title,parent_id as fid')->select();

			$result = [];
			foreach ($level1 as $key1 => $item) {
				$children = GoodCategoryModel::where('status', 1)->where('parent_id', $item['id'])->field('id,title,parent_id as fid')->select();

				$first = $item->toArray();
//				$first['children'] = $children->toArray();
				foreach ($children as $key2 => $child) {
					$children2 = GoodCategoryModel::where('status', 1)->where('parent_id', $child['id'])->field('id,title,parent_id as fid')->select();
					$secend = $child->toArray();
					if (!$children2->isEmpty()) {
						// 必须三级分类
						$secend['children'] = $children2->toArray();
						$first['children'][] = $secend;
					}else{
						$first['children'][] = $secend;
					}
				}
//				if (isset($first['children'])) {
					$result[] = $first;
//				}
			}
			return $result;
		}

	}
